<template>
  <div class='container'>
    <el-card class="box-card">
      <div class="headers">
        <!-- 搜索表单 -->
        <el-form :inline="true" class="demo-form-inline" size="small">
          <el-form-item label="关键词">
            <el-input v-model="query.keyword" placeholder="根据文章标题搜索"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="query.state" placeholder="请选择">
              <el-option label="已启用" value="1"></el-option>
              <el-option label="已禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="clearSearch">清除</el-button>
            <el-button type="primary" @click="toSearch">搜索</el-button>
          </el-form-item>
        </el-form>
        <div class="right">
          <el-button type="success" icon="el-icon-edit" size="small" @click="articlesDialog= true">创建标签</el-button>
        </div>
      </div>
      <!-- 提示条 -->
      <div class="promptBox">
        <i class="el-icon-info"></i> 数据一共{{total}}条
      </div>
      <!-- 表格 -->
      <el-table :data="tableList" stripe highlight-current-row style="width: 100%">
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column label="文章标题" width="380">
          <template #default="{row}">
            {{row.title}}&nbsp;
            <i v-if="row.videoURL" class="el-icon-film" @click="showVidio(row.videoURL)"></i>
          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅读数" />
        <el-table-column prop="username" label="录入人" />
        <el-table-column label="录入时间">
          <template #default="{row}">
            {{row.addDate | timer}}
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template #default="{row}">
            {{row.state===1?"已启用":"已禁用"}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button type="text" @click="lookArts(row.id,row.username)">预览</el-button>
            <el-button type="text" @click="tochangeState({id:row.id,state:row.state===1?0:1})">{{row.state===1?'禁用':'启用'}}</el-button>
            <el-button type="text" :disabled="row.state===1" @click="updataAets(row.id)">修改</el-button>
            <el-button type="text" :disabled="row.state===1" @click="delAtes(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="query.page" :page-sizes="[10, 20, 30, 40]" :page-size="query.pagesize" layout="prev, pager, next,sizes, jumper" :total="total">
      </el-pagination>
    </el-card>
    <!-- 视频播放 -->
    <div class="mask" v-if="videoState" >
      <i @click="masksCloseFun" class="el-icon-circle-close back"></i>
    </div>
    <video ref="video" class="videomasks" v-if="videoState" controls :src="videoUrl" autoplay />
    <!-- 添加修改文章弹窗 -->
    <articles-add ref="articles" :articlesDialog="articlesDialog" @colseDialog="colseDialog" />
    <!-- 预览文章弹窗 -->
    <preview-art ref="previewArts" :PerviewDialog="PerviewDialog" @closePerviewDialog="closePerviewDialog" />
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { list, changeState, remove } from '@/api/hmmm/articles.js'
import articlesAdd from '../components/articles-add.vue'
import previewArt from '../components/articles-preview.vue'
import videoSP from '@/assets/277003.mp4'
export default {
  data () {
    return {
      tableList: [],
      query: {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: null
      },
      total: 0,
      videoUrl: videoSP,
      videoState: false,
      articlesDialog: false,
      PerviewDialog: false
    }
  },
  components: {
    articlesAdd,
    previewArt
  },
  filters: {
    timer (val) {
      return dayjs(val).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  created () {
    this.getList()
  },
  methods: {
    async getList () {
      const { data: res } = await list(this.query)
      // console.log(res)
      this.tableList = res.items
      this.total = res.counts
    },
    clearSearch () {
      this.query = {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: null
      }
      this.getList()
    },
    async toSearch () {
      const { data: res } = await list(this.query)
      // console.log(res)
      this.tableList = res.items
      this.total = res.counts
    },
    async tochangeState ({ id, state }) {
      const { data: res } = await changeState({ id, state })
      if (res) {
        this.$message.success('修改状态成功')
        this.getList()
      } else {
        this.$message.success('修改状态失败，请重试～～～')
      }
    },
    handleSizeChange (val) {
      // console.log(val)
      this.query.pagesize = val
      this.getList()
    },
    handleCurrentChange (val) {
      // console.log(val)
      this.query.page = val
      this.getList()
    },
    showVidio (url) {
      // console.log(url)
      this.videoUrl = url
      this.videoState = true
    },
    masksCloseFun () {
      this.videoState = false
    },
    colseDialog () {
      this.articlesDialog = false
      this.getList()
    },
    updataAets (id) {
      this.$refs.articles.getArticles(id)
      this.articlesDialog = true
    },
    delAtes (id) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        type: 'warning'
      }).then(async () => {
        const { data: res } = await remove(id)
        // console.log(res)
        if (res.success) {
          this.$message.success('删除成功!')
          this.getList()
        }
      }).catch(() => { })
    },
    closePerviewDialog () {
      this.PerviewDialog = false
    },
    lookArts (id, username) {
      this.PerviewDialog = true
      this.$refs.previewArts.showArticles(id, username)
    }

  }
}
</script>

<style scoped lang='scss'>
.container {
  width: 99%;
  margin: 0 auto;
  margin-top: 10px;
}
::v-deep .is-link {
  color: #606266;
}
::v-deep .el-card__body {
  padding-top: 10px;
}
.clearfix {
  font-size: 14px;
}
.el-form {
  float: left;
  margin-left: 15px;
  .el-form-item:nth-child(2) {
    margin-left: 40px;
  }
}
.right {
  float: right;
}
.headers {
  overflow: hidden;
  margin-top: 10px;
}
.promptBox {
  background-color: #f4f4f5;
  height: 35px;
  margin-bottom: 15px;
  padding: 8px 16px;
  font-size: 13px;
  line-height: 18px;
  color: #909399;
  border-radius: 5px;
}
::v-deep.el-table th.el-table__cell.is-leaf {
  background-color: #fafafa;
  border-bottom: 2px solid #e8e8e8;
}
.el-pagination {
  margin: 10px 0;
  float: right;
}
::v-deep .el-table__row i {
  color: #00f;
}
.mask {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 10;
  background-color: #000;
  opacity: 0.6;
  .back{
    position: fixed;
    top: 110px;
    left: 50%;
    transform: translate(-50%,0);
    z-index: 15;
    font-size: 80px;
    color: #ccc;
  }
}
.videomasks {
  max-width: 1200px;
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 20;
}
</style>
